package com.www.applyword.ui.my;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.fragment.HaveReleaseFragment;
import com.www.applyword.fragment.HaveReleaseFragment2;
import com.www.applyword.fragment.HaveReleaseFragment3;
import com.www.applyword.fragment.HaveReleaseFragment4;
import com.www.applyword.fragment.HaveReleaseFragment5;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2019/10/22 0022 15:26
 * version: 1.0
 */
public class MyJobTimeActivity extends BaseActivity {
    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.view_all)
    View viewAll;
    @BindView(R.id.ll_all)
    LinearLayout llAll;
    @BindView(R.id.tv_2)
    TextView tv2;
    @BindView(R.id.view_2)
    View view2;
    @BindView(R.id.ll_2)
    LinearLayout ll2;
    @BindView(R.id.tv_3)
    TextView tv3;
    @BindView(R.id.view_3)
    View view3;
    @BindView(R.id.ll_3)
    LinearLayout ll3;
    @BindView(R.id.tv_4)
    TextView tv4;
    @BindView(R.id.view_4)
    View view4;
    @BindView(R.id.ll_4)
    LinearLayout ll4;
    @BindView(R.id.tv_5)
    TextView tv5;
    @BindView(R.id.view_5)
    View view5;
    @BindView(R.id.ll_5)
    LinearLayout ll5;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> tabs = new ArrayList<>();
    private int type;
    private boolean flag;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.my_job_activity);
        setTitle("我的兼职");
        initData();
    }

    private void initData() {
        type = getIntent().getIntExtra("type", 0);
        Constants.Order_Type = 20;
        if (type == 0){
            Constants.Order_Type = 0;
        }else if (type == 1){
            Constants.Order_Type = 1;
        }else if (type == 2){
            Constants.Order_Type = 2;
        }else {
            Constants.Order_Type = 3;
        }
        tabs.clear();
        fragments.clear();
        tabs.add("全部");
        tabs.add("已报名");
        tabs.add("进行中");
     //   tabs.add("开始工作");
        tabs.add("已结束");
        fragments.add(new HaveReleaseFragment5());
        fragments.add(new HaveReleaseFragment());
        fragments.add(new HaveReleaseFragment2());
        fragments.add(new HaveReleaseFragment3());
      //  fragments.add(new HaveReleaseFragment4());

        //设置TabLayout的模式
      //  tabLayout.setTabMode(TabLayout.MODE_FIXED);
        viewPager.setAdapter(new TabAdapter(getSupportFragmentManager()));
        //关联ViewPager和TabLayout
      //  tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(type);
        viewPager.setOffscreenPageLimit(fragments.size()-1);
        flag = true;

    }

    @Override
    protected void loadData() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (type != 0 && flag){
                    switch (position) {
                        case 1:
                            tvAll.setSelected(false);
                            tv2.setSelected(true);
                            tv3.setSelected(false);
                            tv4.setSelected(false);
                            tv5.setSelected(false);
                            view2.setVisibility(View.VISIBLE);
                            viewAll.setVisibility(View.INVISIBLE);
                            view3.setVisibility(View.INVISIBLE);
                            view4.setVisibility(View.INVISIBLE);
                            view5.setVisibility(View.INVISIBLE);
                            flag = false;
                            break;
                        case 2:
                            viewAll.setSelected(false);
                            tv2.setSelected(false);
                            tv3.setSelected(true);
                            tv4.setSelected(false);
                            tv5.setSelected(false);
                            viewAll.setVisibility(View.INVISIBLE);
                            view2.setVisibility(View.INVISIBLE);
                            view3.setVisibility(View.VISIBLE);
                            view4.setVisibility(View.INVISIBLE);
                            view5.setVisibility(View.INVISIBLE);
                            flag = false;
                            break;
                        case 3:
                            viewAll.setSelected(false);
                            tv2.setSelected(false);
                            tv3.setSelected(false);
                            tv4.setSelected(true);
                            tv5.setSelected(false);
                            viewAll.setVisibility(View.INVISIBLE);
                            view2.setVisibility(View.INVISIBLE);
                            view3.setVisibility(View.INVISIBLE);
                            view4.setVisibility(View.VISIBLE);
                            view5.setVisibility(View.INVISIBLE);
                            flag = false;
                            break;
                        case 4:
                            viewAll.setSelected(false);
                            tv2.setSelected(false);
                            tv3.setSelected(false);
                            tv4.setSelected(false);
                            tv5.setSelected(true);
                            viewAll.setVisibility(View.INVISIBLE);
                            view2.setVisibility(View.INVISIBLE);
                            view3.setVisibility(View.INVISIBLE);
                            view4.setVisibility(View.INVISIBLE);
                            view5.setVisibility(View.VISIBLE);
                            flag = false;
                            break;
                        default:
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        tvAll.setSelected(true);
                        tv2.setSelected(false);
                        tv3.setSelected(false);
                        tv4.setSelected(false);
                        tv5.setSelected(false);
                        viewAll.setVisibility(View.VISIBLE);
                        view2.setVisibility(View.INVISIBLE);
                        view3.setVisibility(View.INVISIBLE);
                        view4.setVisibility(View.INVISIBLE);
                        view5.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        tvAll.setSelected(false);
                        tv2.setSelected(true);
                        tv3.setSelected(false);
                        tv4.setSelected(false);
                        tv5.setSelected(false);
                        view2.setVisibility(View.VISIBLE);
                        viewAll.setVisibility(View.INVISIBLE);
                        view3.setVisibility(View.INVISIBLE);
                        view4.setVisibility(View.INVISIBLE);
                        view5.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        viewAll.setSelected(false);
                        tv2.setSelected(false);
                        tv3.setSelected(true);
                        tv4.setSelected(false);
                        tv5.setSelected(false);
                        viewAll.setVisibility(View.INVISIBLE);
                        view2.setVisibility(View.INVISIBLE);
                        view3.setVisibility(View.VISIBLE);
                        view4.setVisibility(View.INVISIBLE);
                        view5.setVisibility(View.INVISIBLE);
                        break;
                    case 3:
                        viewAll.setSelected(false);
                        tv2.setSelected(false);
                        tv3.setSelected(false);
                        tv4.setSelected(true);
                        tv5.setSelected(false);
                        viewAll.setVisibility(View.INVISIBLE);
                        view2.setVisibility(View.INVISIBLE);
                        view3.setVisibility(View.INVISIBLE);
                        view4.setVisibility(View.VISIBLE);
                        view5.setVisibility(View.INVISIBLE);
                        break;
                    case 4:
                        viewAll.setSelected(false);
                        tv2.setSelected(false);
                        tv3.setSelected(false);
                        tv4.setSelected(false);
                        tv5.setSelected(true);
                        viewAll.setVisibility(View.INVISIBLE);
                        view2.setVisibility(View.INVISIBLE);
                        view3.setVisibility(View.INVISIBLE);
                        view4.setVisibility(View.INVISIBLE);
                        view5.setVisibility(View.VISIBLE);
                        break;
                    default:
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.ll_all, R.id.ll_2, R.id.ll_3, R.id.ll_4, R.id.ll_5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_all:
                viewPager.setCurrentItem(0);
                tvAll.setSelected(true);
                tv2.setSelected(false);
                tv3.setSelected(false);
                tv4.setSelected(false);
                tv5.setSelected(false);
                viewAll.setVisibility(View.VISIBLE);
                view2.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.INVISIBLE);
                view4.setVisibility(View.INVISIBLE);
                view5.setVisibility(View.INVISIBLE);
                break;
            case R.id.ll_2:
                viewPager.setCurrentItem(1);
                tvAll.setSelected(false);
                tv2.setSelected(true);
                tv3.setSelected(false);
                tv4.setSelected(false);
                tv5.setSelected(false);
                view2.setVisibility(View.VISIBLE);
                viewAll.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.INVISIBLE);
                view4.setVisibility(View.INVISIBLE);
                view5.setVisibility(View.INVISIBLE);
                break;
            case R.id.ll_3:
                viewPager.setCurrentItem(2);
                viewAll.setSelected(false);
                tv2.setSelected(false);
                tv3.setSelected(true);
                tv4.setSelected(false);
                tv5.setSelected(false);
                viewAll.setVisibility(View.INVISIBLE);
                view2.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.VISIBLE);
                view4.setVisibility(View.INVISIBLE);
                view5.setVisibility(View.INVISIBLE);
                break;
            case R.id.ll_4:
                viewPager.setCurrentItem(3);
                viewAll.setSelected(false);
                tv2.setSelected(false);
                tv3.setSelected(false);
                tv4.setSelected(true);
                tv5.setSelected(false);
                viewAll.setVisibility(View.INVISIBLE);
                view2.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.INVISIBLE);
                view4.setVisibility(View.VISIBLE);
                view5.setVisibility(View.INVISIBLE);
                break;
            case R.id.ll_5:
                viewPager.setCurrentItem(4);
                viewAll.setSelected(false);
                tv2.setSelected(false);
                tv3.setSelected(false);
                tv4.setSelected(false);
                tv5.setSelected(true);
                viewAll.setVisibility(View.INVISIBLE);
                view2.setVisibility(View.INVISIBLE);
                view3.setVisibility(View.INVISIBLE);
                view4.setVisibility(View.INVISIBLE);
                view5.setVisibility(View.VISIBLE);
                break;
            default:
        }
    }

    class TabAdapter extends FragmentPagerAdapter {
        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //显示标签上的文字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabs.get(position);
        }
    }
}
